<?php	
	require_once 'models/models_hoadon.php';
	$mod = $_GET['mod'];
	$obHD = new models_hoadon();
	require_once 'models/models_khachhang.php';
	$obKH = new models_khachhang();
	require_once 'models/models_cthoadon.php';
	$obcthd=new models_cthoadon();
	require_once 'models/models_mathang.php';
	$obmh=new models_mathang();
	switch ($mod){
		case 'hoadon':{
			$data['listhd'] = $obHD->_listHoaDonn();
			$data['listkh'] = $obKH-> listkh();
			require_once 'views/admin/hoadon/index.php';
			break;
		}
		case 'add_hoadon':{
			if($_POST){
				$obHD->_addHoaDon($_POST['cmb_khachhang'], $_POST['ngaylap'], $_POST['nguoilap']);
				echo '<script>window.location.href="admin.php?mod=hoadon"</script>';
			}
			$data['listkh'] = $obKH->listkh();
			require_once 'views/admin/hoadon/add.php';
			break;
		}
		case 'del_hoadon':{
			$obHD->_delHoaDon($_GET['id']);
			echo '<script>window.location.href="admin.php?mod=hoadon"</script>';
			break;
		}
		case 'edit_hoadon':{
			if($_POST){
				$obHD->_editHoaDon($_POST['TinhTrang'], $_GET['id']);
				echo '<script>window.location.href="admin.php?mod=hoadon"</script>';
			}
			$data = $obHD->_getHoaDon($_GET['id']);
			$data['listkh'] = $obKH->listkh();
			require 'views/admin/hoadon/edit.php';
			break;
		}
		case 'chitiethoadon':{
			$data = $obHD->_getHoaDon($_GET['id']);	
			$data['listkh'] = $obKH->listkh();
			$data['cthd']=$obcthd->getCTHoaDon($_GET['id']);
			$data['listmh']=$obmh->listMatHang();
			require 'views/admin/hoadon/chitiethoadon.php';
			break;
		}
		case 'inhoadon':{
			$data = $obHD->_getHoaDon($_GET['id']);	
			$kh = $obKH->_getKhachHang($data['MaKH']);
			$data['cthd']=$obcthd->getCTHoaDon($_GET['id']);
			$data['listmh']=$obmh->listMatHang();
			require_once 'views/admin/hoadon/inhoadon.php';
			break;
		}
	case 'editchitiethd':{
			$data['listmh']=$obmh->listMatHang();
			$cthd=$obcthd->_getCTHoaDon($_GET['id']);
			if($_POST){
				if($cthd['MaMH']==$_POST['cmb_MatHang'])
				{
				$mh=$obmh->getMatHang($_POST['cmb_MatHang']);
				
				if(($mh['SoLuong']+$cthd['SoLuongBan'])>=$_POST['SoLuong'])
				{
					
				$soluong=$mh['SoLuong']+$cthd['SoLuongBan']-$_POST['SoLuong'];
				$obmh->updatemh($soluong,$_POST['cmb_MatHang']);
				$obcthd->_editCTHoaDon($_POST['cmb_MatHang'],$_POST['SoLuong'], $mh['Gia'], $_GET['id']);
				}
				else{
					$soluong=$mh['SoLuong']+$cthd['SoLuongBan'];
					$obmh->updatemh(0, $_POST['cmb_MatHang']);
					$obcthd->_editCTHoaDon($_POST['cmb_MatHang'],$soluong, $mh['Gia'], $_GET['id']);
				}
				
				
				echo '<script>window.location.href="admin.php?mod=hoadon"</script>';
				}
				else
				{
					$mathang=$obmh->getMatHang($cthd['MaMH']);
					$sl=$mathang['SoLuong']+$cthd['SoLuongBan'];
					$obmh->updatemh($sl,$cthd['MaMH']);
					$mh=$obmh->getMatHang($_POST['cmb_MatHang']);
				if($mh['SoLuong']>=$_POST['SoLuong'])
				{
					$soluong=$mh['SoLuong']-$_POST['SoLuong'];
					$obcthd->_editCTHoaDon($_POST['cmb_MatHang'],$_POST['SoLuong'], $mh['Gia'], $_GET['id']);
					$obmh->updatemh($soluong, $_POST['cmb_MatHang']);
				}
				else{
					$soluong=$mh['SoLuong'];
					$obcthd->_editCTHoaDon($_POST['cmb_MatHang'],$soluong, $mh['Gia'], $_GET['id']);
					$obmh->updatemh(0, $_POST['cmb_MatHang']);
				}
					
					
					echo '<script>window.location.href="admin.php?mod=hoadon"</script>';
					
				}
				
			
			}
			require 'views/admin/hoadon/editchitiethd.php';
			break;
		}
		case 'del_cthoadon':{
			$cthd=$obcthd->_getCTHoaDon($_GET['id']);
			$mh=$obmh->getMatHang($cthd['MaMH']);
			$soluong=$mh['SoLuong']+$cthd['SoLuongBan'];
			$obmh->updatemh($soluong,$cthd['MaMH']);
			$obcthd->_delCTHoaDon($_GET['id']);
			$obHD->_delHoaDon($cthd['MaHD']);
			echo '<script>window.location.href="admin.php?mod=hoadon"</script>';
			break;
		}
	}
?>